package com.nogroup.springbootproject.service.impl;

import com.nogroup.springbootproject.dto.ExamDto;
import com.nogroup.springbootproject.entity.Exam;
import com.nogroup.springbootproject.dao.ExamDao;
import com.nogroup.springbootproject.service.ExamService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Exam)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 18:45:33
 */
@Service("examService")
@Transactional(rollbackFor = Exception.class)
public class ExamServiceImpl implements ExamService {
    @Resource
    private ExamDao examDao;

    /**
     * 通过ID查询单条数据
     *
     * @param examYear 主键
     * @return 实例对象
     */
    @Override
    public Exam queryById(String examYear) {
        return this.examDao.queryById(examYear);
    }

    /**
     * 分页查询
     *
     * @param exam 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Exam> queryByPage(Exam exam, PageRequest pageRequest) {
        long total = this.examDao.count(exam);
        return new PageImpl<>(this.examDao.queryAllByLimit(exam, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param exam 实例对象
     * @return 实例对象
     */
    @Override
    public Exam insert(Exam exam) {
        System.out.println("ServiceImpl:"+exam.toString());
        this.examDao.insert(exam);
        return exam;
    }

    /**
     * 修改数据
     *
     * @param exam 实例对象
     * @return 实例对象
     */
    @Override
    public Exam update(Exam exam) {
        this.examDao.update(exam);
        return this.queryById(exam.getExamYear());
    }

    /**
     * 通过主键删除数据
     *
     * @param exam 主键
     * @return 是否成功
     */
    @Override
    public Exam deleteById(Exam exam) {
        this.examDao.deleteById(exam);
        return exam;
    }

    /**
     * 获取所有考试项
     * @return
     */
    @Override
    public List<Exam> getAllExams() {
        return examDao.getAllExams();
    }

    /**
     * 对考试项进行模糊查询
     * @param examDto
     * @return
     */
    @Override
    public List<Exam> getExamsByFuzzySearch(ExamDto examDto) {
        return examDao.getExamsByFuzzySearch(examDto);
    }

    @Override
    public Long countPage(ExamDto examDto) {
        return examDao.CountPage(examDto);
    }



}
